package com.xjtu.se.homework.client.mainPage;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.xjtu.se.homework.client.FileProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.UUID;

@RestController
@RequestMapping("/mainPage")
public class PicAPI {
//    private static final Path fileStorageLocation = Paths.get(fileProperties.getUploadDir()).toAbsolutePath().normalize(); // 文件在本地存储的地址

//    public PicAPI(FileProperties fileProperties) throws Exception {
//        this.fileStorageLocation = ;
//        try {
//            Files.createDirectories(this.fileStorageLocation);
//        } catch (Exception e) {
//            throw new Exception("Could not create the directory where the uploaded files will be stored.", e);
//        }
//    }

    private static String filePath = "C:/Users/wyh/Desktop/helper-master/homework_helper/uploads";
    private static String search_image = null;
    private static ArrayList flag = new ArrayList();

    @Resource
    private PicService picService;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date time;


    @PostMapping("/items")
    public String handle(@RequestParam("items") String items,
                         @RequestParam("u_id") String u_id,
                         @RequestParam("dev_id") String dev_id,
                         @RequestParam("ques_class") String ques_class,
                         @RequestParam("ans_num") String ans_num){
        while (flag.size() == 0);
        PicDto picDto = new PicDto();
        picDto.setU_id(Integer.valueOf(u_id));
        picDto.setItems(items);
        picDto.setSearch_image(search_image);
        picDto.setAns_num(Integer.valueOf(ans_num));
        picDto.setQues_class(Integer.parseInt(ques_class));
        picDto.setDev_id(Integer.valueOf(dev_id));
        time = new Date(System.currentTimeMillis());
        long id = System.currentTimeMillis();
        picDto.setSearch_id(id + u_id + "");
        picDto.setRequest_id(id + u_id + "");
        picDto.setSearch_time(time);
        picService.enterLog(picDto);
        picService.enterHistory(picDto);
        return items;
    }


    public static String getUUIDFileName(String fileName) {
        // 将文件名的前面部分进行截取：xx.jpg   --->   .jpg
        int idx = fileName.lastIndexOf(".");
        String extention = fileName.substring(idx);
        String uuidFileName = UUID.randomUUID().toString().replace("-","") + extention;
        return uuidFileName;
    }

    @PostMapping("/solution")
    public static String fileUpload(@RequestParam(value = "originalData") MultipartFile file) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }

        System.out.println("文件开始上传");
        String fileName = getUUIDFileName(file.getOriginalFilename());  // 文件名
        System.out.println(fileName);
        //上传到服务器之中了
//        String filePath = request.getSession().getServletContext().getRealPath("imgupload/");

        File dest = new File(filePath +"/"+ fileName);
        System.out.println("------------------------------------------");
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
            try{
                dest.createNewFile();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        search_image = fileName;
        System.out.println("::"+search_image);
        flag.add(0);
        return null;
    }
}

